import os
from shutil import copy

datapath = "./256_ObjectCategories"
dirs = os.listdir(datapath)
for dir in dirs:
    classpath = os.path.join(datapath, dir)
    if not os.path.isdir("./data/train/" + dir):
        os.makedirs("./data/train/" + dir)
    if not os.path.isdir("./data/val/" + dir):
        os.makedirs("./data/val/" + dir)
    imgs = os.listdir(classpath)
    print("image num:", len(imgs))
    length = len(imgs)
    train_num = int(length * 0.8)
    test_num = len(imgs) - train_num
    for i in range(train_num):
        train_imgpath = os.path.join(classpath, imgs[i])
        if os.path.isdir(train_imgpath):
            print("dir")
            continue
        copy(train_imgpath, "./data/train/" + dir)
    for i in range(train_num, length):
        val_imgpath = os.path.join(classpath, imgs[i])
        if os.path.isdir(val_imgpath):
            print("dir")
            continue
        copy(val_imgpath, "./data/val/" + dir)
